package edu.ciee.tz.action;


import java.util.List;

import javax.annotation.Resource;

import edu.ciee.tz.dao.YuangongDAO;
import edu.ciee.tz.dao.ZhibieDAO;
import edu.ciee.tz.model.Yuangong;
import edu.ciee.tz.model.Zhibie;

public class YuangongAction {
	private int id;
	private String name;
	private String sex;
	private String birthday;

	private int zhibie;
	private String tele;
	
	private String tip;
	
	private List resultList;	
	private YuangongDAO yuangongDAO;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public int getZhibie() {
		return zhibie;
	}
	public void setZhibie(int zhibie) {
		this.zhibie = zhibie;
	}
	public String getTele() {
		return tele;
	}
	public void setTele(String tele) {
		this.tele = tele;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public List getResultList() {
		return resultList;
	}
	public void setResultList(List resultList) {
		this.resultList = resultList;
	}
	public YuangongDAO getYuangongDAO() {
		return yuangongDAO;
	}
	
	@Resource
	public void setYuangongDAO(YuangongDAO yuangong) {
		this.yuangongDAO = yuangong;
	}	
	
	@SuppressWarnings("unchecked")
	public String forLuru(){
			return "success";			
	}
	
	public String insert(){
		try{
			Yuangong yg=new Yuangong();
			yg.setBirthday(this.getBirthday());
			yg.setName(this.getName());
			yg.setSex(this.getSex());
			yg.setTele(this.getTele());			
			Zhibie zb=new Zhibie();			
			zb.setId(this.getZhibie());
			
			yg.setZhibie(zb);
			System.out.println(this.getZhibie());
			this.getYuangongDAO().insert(yg);
			
			//ActionContext.getContext().getSession().put("yuangongSession", this.yuangongDAO.searchAll());
			return "success";			
		}catch(Exception e){
			this.setTip(e.toString());
			e.printStackTrace();
			return "error";
		}		
	}
	
	public String search(){
		try{
			Yuangong yg=new Yuangong();
			yg.setId(this.getId());
			yg.setBirthday(this.getBirthday());
			yg.setName(this.getName());
			yg.setSex(this.getSex());
			yg.setTele(this.getTele());
			
			Zhibie zb=new Zhibie();
			zb.setId(this.getZhibie());		
			
			yg.setZhibie(zb);
			
			//yg.setZhibie(zb);			
			
			this.setResultList(this.getYuangongDAO().search(yg));
			
			return "success";
		}catch(Exception e){
			this.setTip(e.toString());
			return "error";
		}
	}
	
	public String delete(){
		try{
			Yuangong yg=new Yuangong();
			yg.setId(this.getId());
			yg.setBirthday(this.getBirthday());
			yg.setName(this.getName());
			yg.setSex(this.getSex());
			yg.setTele(this.getTele());
			
			Zhibie zb=new Zhibie();
			zb.setId(this.getZhibie());
			
			yg.setZhibie(zb);			
			this.getYuangongDAO().delete(yg);
			
			//ActionContext.getContext().getSession().put("yuangongSession", this.yuangongDAO.searchAll());
			return "refresh";
		}catch(Exception e){
			this.setTip(e.toString());
			e.printStackTrace();
			return "error";
		}
	}
	
	public String forEdit(){
		return "success";
	}
	public String edit(){
		try{
			Yuangong yg=new Yuangong();
			yg.setId(this.getId());
			yg.setBirthday(this.getBirthday());
			yg.setName(this.getName());
			yg.setSex(this.getSex());
			yg.setTele(this.getTele());

			Zhibie zb=new Zhibie();
			zb.setId(this.getZhibie());
			yg.setZhibie(zb);		
			
			this.getYuangongDAO().update(yg);
			
			//ActionContext.getContext().getSession().put("yuangongSession", this.yuangongDAO.searchAll());
			return "refresh";
		}catch(Exception e){
			this.setTip(e.toString());
			e.printStackTrace();
			return "error";
		}
	}
}
